package com.xy.service.mainData;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xy.api.model.dto.mainData.ContactDTO;
import com.xy.api.model.request.mainData.ContactRequest;
import com.xy.api.model.response.Rows;
import com.xy.api.service.mainData.IContactService;
import com.xy.auth.UserContext;
import com.xy.mapper.mainData.ContactMapper;
import com.xy.model.po.mainData.ContactPO;
import com.xy.utils.CustomBeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

@Slf4j
@Service
public class ContactServiceImpl implements IContactService {

    @Autowired
    private ContactMapper contactMapper;

    @Override
    public Rows<ContactDTO> queryContactList(ContactRequest request) {
        Rows<ContactDTO> result = new Rows<>();
        LambdaQueryWrapper<ContactPO> contactWrapper = new LambdaQueryWrapper<>();
        contactWrapper
                .eq(ContactPO::getIsDeleted, false)
                .eq(ContactPO::getType, request.getType())
                .eq(ContactPO::getBizId, request.getBizId());
        if (StringUtils.hasText(request.getName())) {
            contactWrapper.like(ContactPO::getName, "%" + request.getName() + "%");
        }
        int pageSize = null != request.getPageSize() ? request.getPageSize() : 10;
        int pageNum = null != request.getPageNum() ? request.getPageNum() : 1;
        Page<ContactPO> contactPOPage = new Page<>(pageNum, pageSize);
        contactPOPage = contactMapper.selectPage(contactPOPage, contactWrapper);

        result.setTotal(contactPOPage.getTotal());
        result.setRows(CustomBeanUtil.copyListProperties(contactPOPage.getRecords(), ContactDTO::new));
        return result;
    }

    @Override
    public ContactDTO contactDetail(Long id) {
        Assert.notNull(id, "联系人ID必填");
        ContactDTO contact = new ContactDTO();
        ContactPO contactPO = contactMapper.selectById(id);
        CustomBeanUtil.copyProperties(contactPO, contact);
        return contact;
    }

    @Override
    public void editContact(ContactDTO request) {
        ContactPO contact = new ContactPO();
        CustomBeanUtil.copyProperties(request, contact);
        if (null != contact.getId()) {
            updateContact(contact);
        } else {
            addContact(contact);
        }
    }

    private void updateContact(ContactPO contact) {
        Assert.notNull(contact.getId(), "联系人ID必填");
        contact.setUpdateBy(UserContext.get().getId());
        contactMapper.updateById(contact);
    }

    private void addContact(ContactPO contact) {
        contact.setCreateBy(UserContext.get().getId());
        contactMapper.insert(contact);
    }

    @Override
    public void deleteContact(Long id) {
        Assert.notNull(id, "联系人ID必填");
        ContactPO deleteRecord = new ContactPO();
        deleteRecord.setId(id);
        deleteRecord.setIsDeleted(1);
        deleteRecord.setUpdateBy(UserContext.get().getId());
        contactMapper.updateById(deleteRecord);
    }
}
